﻿#include <iostream>
using namespace std;
#include<string>
#include"sqlite3/sqlite3.h"

#define MAX 1000

//菜单界面
void showMenu()
{
	cout << "\t**************" << endl;
	cout << "\t*1.添加联系人*" << endl;
	cout << "\t*2.显示联系人*" << endl;
	cout << "\t*3.删除联系人*" << endl;
	cout << "\t*4.查找联系人*" << endl;
	cout << "\t*5.修改联系人*" << endl;
	cout << "\t*6.清空联系人*" << endl;
	cout << "\t*0.退出通讯录*" << endl;
	cout << "\t**************" << endl;
}

//联系人结构体
struct Person
{
	string m_Name;
	int m_Sex;			//性别，1男，2女
	int m_Age;
	string m_Phone;
	string m_Addr;		//住址
};

//通讯录结构体
struct Addressbooks
{
	//通讯录人数上限
	struct Person personArray[MAX];
	//通讯录当前记录的联系人个数
	int m_size;
};


//1.添加联系人
void addPerson(Addressbooks *abs)
{

	//判断通讯录是否已满，满了则不再添加
	if (abs->m_size == MAX)
	{
		cout << "通讯录已满,无法操作" << endl;
		return;
	}
	else
	{
		string name;
		cout << "请输入姓名：" << endl;
		cin >> name;
		abs->personArray[abs->m_size].m_Name = name;
		cout << "请输入性别：\n\t1 -- 男\n\t2 -- 女" << endl;
		int sex;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[abs->m_size].m_Sex = sex;
				break;
			}
			cout << "性别输入有误：" << endl;
		}
		cout << "请输入年龄：" << endl;
		int age;
		while (true)
		{
			cin >> age;
			if (0 <= age && age <= 120)
			{
				abs->personArray[abs->m_size].m_Age = age;
				break;
			}
			cout << "年龄输入有误！" << endl;
		}
		cout << "请输入联系电话：" << endl;
		string phone;
		cin >> phone;
		abs->personArray[abs->m_size].m_Phone = phone;
		cout << "请输入家庭地址：" << endl;
		string addr;  //addr的数据类型是字符型，写代码时注意数据类型
		cin >> addr;
		abs->personArray[abs->m_size].m_Addr = addr;
		abs->m_size++;
		cout << "\n添加成功！" << endl;

	}
}

//2.显示联系人
void showPerson(Addressbooks *abs)
{
	if (abs->m_size == 0)
	{
		cout << "通讯录为空！" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_size; i++)
		{
			cout << "姓名：" << abs->personArray[i].m_Name << "\t"
				<< "性别：" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t"
				<< "年龄：" << abs->personArray[i].m_Age << "\t"
				<< "联系电话：" << abs->personArray[i].m_Phone << "\t"
				<< "家庭住址：" << abs->personArray[i].m_Addr << endl;
		}
	}
}

//判断需要删除的联系人是否存在于通讯录中
int isExist(Addressbooks * abs, string name)
{
	for (int i = 0; i < abs->m_size; i++)
	{
		if (abs->personArray[i].m_Name == name)
			return i;
	}
	return -1;
}

//3.删除联系人
void deletePerson(Addressbooks *abs)
{
	cout << "请输入要删除的联系人姓名：" << endl;
	string name;
	cin >> name;
	int res = isExist(abs, name);  //注意此处abs变量不需要取地址符，其在函数isExist中本身就是地址
	if (res == -1)
	{
		cout << "此用户不存在！" << endl;
	}
	else
	{
		//此人存在，开始删除
		for (int i = res; i < abs->m_size; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_size--;   //更新通讯录大小
		cout << "删除成功！" << endl;
	}
}

//4.查找联系人
void findPerson(Addressbooks* abs)
{
	string name;
	while (true)
	{
		cout << "请输入需要查找的联系人姓名：" << endl;
		cin >> name;
		if (int res = isExist(abs, name) == -1)
		{
			cout << "用户不存在" << endl;
		}
		else
		{
			cout << "姓名：" << abs->personArray[res].m_Name << "\t"
				<< "性别：" << (abs->personArray[res].m_Sex == 1 ? "男" : "女") << "\t"
				<< "年龄：" << abs->personArray[res].m_Age << "\t"
				<< "联系电话：" << abs->personArray[res].m_Phone << "\t"
				<< "家庭住址：" << abs->personArray[res].m_Addr << endl;
			break;
		}
	}
}

//5.修改联系人信息
void modifyPerson(Addressbooks *abs)
{
	cout << "*****1.姓名\t*****" << endl;
	cout << "*****2.性别\t*****" << endl;
	cout << "*****3.年龄\t*****" << endl;
	cout << "*****4.联系电话\t*****" << endl;
	cout << "*****5.家庭住址\t*****" << endl;
	cout << "*****0.退出修改\t*****" << endl;
	string name;
	while (true)
	{
		cout << "首先请输入要修改的联系人姓名：" << endl;
		cin >> name;
		if (int res = isExist(abs, name) == -1)
		{
			cout << "此联系人不存在" << endl;
		}
		else
		{
			int sel = 0;
			while (true)
			{
				cout << "请从上面列表选择你要修改的内容：" << endl;
				cin >> sel;
				switch (sel)
				{
				case 1://修改姓名
				{
					string name;
					cout << "请输入姓名：" << endl;
					cin >> name;
					abs->personArray[res].m_Name = name;
					cout << "姓名修改完成" << endl;
					break;
				}
				case 2://修改性别
				{
					cout << "请输入性别：\n\t1 -- 男\n\t2 -- 女" << endl;
					int sex;
					while (true)
					{
						cin >> sex;
						if (sex == 1 || sex == 2)
						{
							abs->personArray[res].m_Sex = sex;
							cout << "性别修改完成" << endl;
							break;
						}
						cout << "性别输入有误：" << endl;
					}
					break;
				}
				case 3://修改年龄
				{
					cout << "请输入年龄：" << endl;
					int age;
					while (true)
					{
						cin >> age;
						if (0 <= age && age <= 120)
						{
							abs->personArray[res].m_Age = age;
							cout << "年龄修改完成" << endl;
							break;
						}
						cout << "年龄输入有误！" << endl;
					}
					break;
				}
				case 4://修改联系电话
				{
					cout << "请输入联系电话：" << endl;
					string phone;
					cin >> phone;
					abs->personArray[res].m_Phone = phone;
					cout << "联系电话修改完成" << endl;
					break;
				}
				case 5://修改家庭住址
				{
					cout << "请输入家庭地址：" << endl;
					string addr;
					cin >> addr;
					abs->personArray[res].m_Addr = addr;
					cout << "家庭住址修改完成" << endl;
					break;
				}
				default:
					break;
				}
				if (sel == 0)
					break;   //退出while(true)死循环
			}
		}
		break;
	}
}

//清空联系人
void cleanPerson(Addressbooks *abs)
{
	abs->m_size = 0;	//大小置0，逻辑清空
	cout << "所有联系人已清空" << endl;
}

int main()
{
	sqlite3* db = NULL;
	const int rc = sqlite3_open("PhoneBook.db", &db);
	sqlite3_exec(db, "CREATE TABLE PhoneBook(name string,sex string,age int,number string,address string)", NULL, NULL, NULL);
	int select = 0;
	//创建通讯录结构体变量
	struct Addressbooks abs;
	abs.m_size = 0;    //初始化为0
	while (true)
	{
		showMenu();		//显示菜单
		cin >> select;
		switch (select)
		{
		case 1://添加联系人
			addPerson(&abs);
			sqlite3_exec(db, "INSERT INTO PhoneBook(name,sex,age,number,address) VALUE(name,sex,age,phone,addr)", NULL, NULL, NULL);
			break;
		case 2://显示联系人
			showPerson(&abs);
			break;
		case 3://删除联系人
		{
			deletePerson(&abs);
			/*cout << "删除联系人：请输入姓名  " << endl;
			string name;
			cin >> name;
			if (isExist(&abs, name) == -1)
			{
				cout << "查无此人" << endl;
			}
			else
			{
				cout << "此人存在于通讯录中，可以删除。" << endl;
			}*/
		}
		break;
		case 4://查找联系人
			findPerson(&abs);
			break;
		case 5://修改联系人
			modifyPerson(&abs);
			break;
		case 6://清空联系人
			cleanPerson(&abs);
			break;
		case 0://退出通讯录系统
			cout << "欢迎下次使用！" << endl;
			return 0;
			break;
		default:
			break;
		}
		system("pause");
		system("cls");
	}
}